<?php

require($_SERVER['DOCUMENT_ROOT'] . "/bootstrap.php");

if (isset($_GET['idproduct']))
	$idproduct = $_GET['idproduct'];
else
	$idproduct = 0;

$productdetail1 = mysql_query(@"
        SELECT *
        FROM product p
        INNER JOIN category c
            ON p.`idcategory` = c.`idcategory`
        WHERE idproduct = $idproduct
    ");
$found = mysql_num_rows($productdetail1);
$smarty->assign("found", $found);
if ($found == 0) {
	$smarty->display('../templates/productdetail/index.tpl');
	die();
}
while ($row = mysql_fetch_array($productdetail1)) {
	$productdetail[] = $row;
}
$smarty->assign("productdetail", $productdetail);

// load các sản phẩm cùng loại
$idcategory_cung_loai = $productdetail[0]['idcategory'];
$sql = @"
		SELECT *
		FROM product p
		INNER JOIN category c
			ON p.`idcategory` = c.`idcategory`
		WHERE c.`idcategory` = $idcategory_cung_loai
		AND p.`idproduct` <> $idproduct
		ORDER BY RAND()
		LIMIT 3
	  ";
//var_dump($sql);
$product_cung_loai1 = mysql_query($sql);
while ($row = mysql_fetch_array($product_cung_loai1)) {
	$product_cung_loai[] = $row;
}
$smarty->assign("product_cung_loai", $product_cung_loai);
// end

$productdetailcolor1 = mysql_query(@"
        SELECT *
        FROM product_color pc
        INNER JOIN color c
            ON pc.`idcolor` = c.`idcolor`
        WHERE idproduct = $idproduct
    ");
while ($row = mysql_fetch_array($productdetailcolor1)) {
	$productdetailcolor[] = $row;
}
if (mysql_num_rows($productdetailcolor1) != 0) {
	$smarty->assign("productdetailcolor", $productdetailcolor);
}

$idcategory = $productdetail[0]['idcategory'];
$productcategoryparent1 = mysql_query(@"
        SELECT c1.`categoryname`, c1.`link`, c2.`categoryname` AS parentname, c2.`link` AS parentlink
        FROM category c1, category c2
        WHERE c1.`parent` = c2.`idcategory` AND c1.`idcategory` = $idcategory
    ");
while ($row = mysql_fetch_array($productcategoryparent1)) {
	$productcategoryparent[] = $row;
}
$smarty->assign("productcategoryparent", $productcategoryparent);


$smarty->display('../templates/productdetail/index.tpl');
?>